﻿package components 
{
	import flash.display.BlendMode;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 * 按钮（普通按钮和选中按钮）
	 * 	分两层：1--按钮状态帧  2--label显示文本（可无）
	 * 	共四帧：1--MouseOut  2--MouseOver MouseUp  3--Select  4--MouseDown
	 * ...
	 * @author firechen
	 * @version	1.0
	 */
	public class Button extends Component 
	{
		//是否是双击
		protected var _isDoubleClick:Boolean = false;
		//判断是是否是双击
		protected var _timer:Timer;
		protected var _mousePoint:Point = new Point();	//鼠标弹起的坐标
		protected var _isMouseClick:Boolean = false;		//是否是鼠标点击事件
		//是否可选
		protected var _isSelect:Boolean = false;
		//是否有鼠标在本按钮上
		protected var _isMouseOver:Boolean = false;
		//鼠标是否按下
		protected var _isMouseDown:Boolean = false;
		
		//选中按钮
		public var toggle:Boolean = false;
		//是否可撤销选中
		public var undoEnabled:Boolean = true;
		//文字显示
		public var textfield:TextField;
		
		public function Button(view:MovieClip, toggle:Boolean = false, undoEnabled:Boolean = true)
		{
			this.toggle = toggle;
			this.undoEnabled = undoEnabled;
			super(view);
		}
		
		/**
		 * 初始化
		 */
		override protected function draw():void 
		{
			view.gotoAndStop(1);
			view.buttonMode = true;
			view.mouseChildren = false;
			textfield = view.getChildByName("label") as TextField;
			if (textfield) textfield.blendMode = BlendMode.LAYER;
			
			view.addEventListener(MouseEvent.CLICK, mouseClickHandle);
			view.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandle);
			view.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandle);
		}
		
		/**
		 * 鼠标点击事件处理
		 * @param	e
		 */
		protected function mouseClickHandle(e:MouseEvent):void 
		{
			if (!_isMouseClick) return;
			if (!view.enabled) return;
			if (view.doubleClickEnabled)
			{
				if (_timer && !_timer.running)
				{
					_timer.start();
				}
			}
			else
			{
				timerHandle();
			}
		}
		
		/**
		 * 双击点击
		 * @param	e
		 */
		protected function mouseDoubleHandle(e:MouseEvent):void 
		{
			if (!view.enabled) return;
			
			this._isDoubleClick = true;
		}
		
		/**
		 * 鼠标按下
		 * @param	e
		 */
		protected function mouseDownHandle(e:MouseEvent):void 
		{
			_isMouseDown = true;
			if(view.stage) view.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandle);
			_mousePoint = new Point(e.stageX, e.stageY);
			if (!view.enabled || _isSelect) return;
			if (view.totalFrames >= 4) view.gotoAndStop(4);
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}
		
		/**
		 * 鼠标弹起
		 * @param	e
		 */
		protected function mouseUpHandle(e:MouseEvent):void 
		{
			_isMouseDown = false;
			if (view&&view.stage) view.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandle);
			
			if (!view.enabled) return;
			
			//检测是否是鼠标单击 还是拖动
			if (Point.distance(_mousePoint, new Point(e.stageX, e.stageY)) >= 10)
			{
				_isMouseClick = false;
			}
			else
			{
				_isMouseClick = true;
			}
			_mousePoint = new Point();
			if (_isMouseOver)
			{
				if(view) view.gotoAndStop(2);
				dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
			}
			else 
			{
				if (!view) return;
				if (view.totalFrames >= 3)
					isSelect ? view.gotoAndStop(3) : view.gotoAndStop(1);
				else
					view.gotoAndStop(1);
			}
		}
		
		/**
		 * 鼠标移入事件
		 * @param	e
		 */
		protected function mouseOverHandle(e:MouseEvent):void 
		{
			_isMouseOver = true;
			view.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandle);
			
			if (!view.enabled) return;
			
			if (!isSelect && !_isMouseDown)
			{
				view.gotoAndStop(2);
			}
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
		}
		
		/**
		 * 鼠标移出事件
		 * @param	e
		 */
		protected function mouseOutHandle(e:MouseEvent):void 
		{
			_isMouseOver = false;
			view.removeEventListener(MouseEvent.ROLL_OUT, mouseOutHandle);
			
			if (!view.enabled) return;
			
			if (!isSelect && !_isMouseDown)
			{
				view.gotoAndStop(1);
			}
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
		}
		
		/**
		 * 检测是否为双击
		 * @param	e
		 */
		protected function timerHandle(e:TimerEvent = null):void 
		{
			if (_isDoubleClick)
			{
				dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK));
			}
			else
			{
				if (toggle)
				{
					if (undoEnabled) isSelect = !isSelect;
					else isSelect = true;
				}
				dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
			_isDoubleClick = false;
		}
		
		/**
		 * 获取 设置文本显示
		 */
		public function get label():String { return textfield ? textfield.text : ""; }
		
		public function set label(value:String):void
		{
			if (value && textfield && textfield.text != value)
			{
				textfield.text = value;
			}
		}
		
		/**
		 * 获取 设置是否可用
		 */
		public function get enabled():Boolean { return view.enabled; }
		
		public function set enabled(value:Boolean):void 
		{
			if (view.enabled != value)
			{
				view.enabled = value;
				view.buttonMode = value;
			}
		}
		
		/**
		 * 设置是否处于选中状态
		 */
		public function get isSelect():Boolean { return _isSelect; }
		
		public function set isSelect(value:Boolean):void 
		{
			_isSelect = value;
			if (_isSelect)
			{
				if (view.totalFrames >= 3)
				{
					view.gotoAndStop(3);
					return;
				}
			}
			if (_isMouseOver) view.gotoAndStop(2);
			else view.gotoAndStop(1);
		}
		
		/**
		 * 获取 设置是否允许双击
		 */
		public function get doubleClickEnabled():Boolean { return view.doubleClickEnabled; }
		
		public function set doubleClickEnabled(value:Boolean):void 
		{
			if (view.doubleClickEnabled != value)
			{
				view.doubleClickEnabled = value;
				if (value)
				{
					view.addEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleHandle);
					if (!_timer)
					{
						_timer = new Timer(250, 1);
						_timer.addEventListener(TimerEvent.TIMER, timerHandle);
					}
				}
				else
				{
					view.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleHandle);
					if (_timer)
					{
						_timer.stop();
						_timer.removeEventListener(TimerEvent.TIMER, timerHandle);
						_timer = null;
					}
				}
			}
		}
		
		/**
		 * 清理
		 */
		override public function clear():void 
		{
			super.clear();
			isSelect = false;
			enabled = true;
			_isMouseDown = false;
			_isMouseOver = false;
			if (_timer)
			{
				_timer.stop();
			}
			label = "";
		}
	}

}